import { createRouter, createWebHistory } from "vue-router";
//引入前端页面路劲
import Home from '../components/home/Home.vue'
	//主界面
	import Main from '../components/home/page/Main.vue'
	//商品详情
	import DetaList from '../components/home/page/DetaList.vue'
	//购物车
	import Order from '../components/home/page/Order.vue'
	//下单页面
	import CratOrder from '../components/home/cart/CratOrder.vue'
	//付款
	import PaymentCrat from '../components/home/cart/PaymentCrat.vue'
	//个人信息设置
	import Edit from '../components/home/page/Edit.vue'
import Cart from '../components/home/cart/Cart.vue'

//引入后台路径
import Admin from '../components/admin/Admin.vue'
	// 类型列表92
	import Type from '../components/admin/page/Type.vue'
	// 类型列表93
	import Type93 from '../components/admin/page/Type93.vue'
	//商品管理
	import Books from '../components/admin/page/Books.vue'
		// 商品添加
		import Bookedit from '../components/admin/page/Bookedit.vue'
	// 热度管理（轮播图）
	import Banner from '../components/admin/page/Banner.vue'
		//添加轮播图
		import AddbannerImg from '../components/admin/page/AddbannerImg.vue'
	//订单页
	import Orders from '../components/admin/page/Orders.vue'
	//用户列表
	import List from '../components/admin/page/List.vue'
	

const routes = [
	{path:'/',redirect:'main'},
	{
		path:'/home',
		name:'Home',
		component:Home,
		children:[
			{path:'/main',name:'main',component:Main},
			{path:'/detaList',name:'detaList',component:DetaList},
			{path:'/order',name:'order',meta:{role:'P'},component:Order},
			{path:'/cart',name:'cart',meta:{role:'P'},component:Cart},
			{path:'/cratorder',name:'cratorder',meta:{role:'P'},component:CratOrder},
			{path:'/paymentcrat',name:'paymentcrat',meta:{role:'P'},component:PaymentCrat},
			{path:'/edit',name:'edit',meta:{role:'P'},component:Edit}
		]
	},
	{
		path:'/admin',
		name:'Admin',
		component:Admin,
		meta:{role:'G'},
		children:[
			{path:'/admin/type',name:'type',component:Type},
			{path:'/admin/type93',name:'type93',component:Type93},
			{path:'/admin/books',name:'books',component:Books},
			{path:'/admin/banner',name:'banner',component:Banner},
			{path:'/admin/orders',name:'orders',component:Orders},
			{path:'/admin/list',name:'list',component:List},
			{path:'/addbannerimg',name:'addbannerimg',component:AddbannerImg},
			{path:'bookedit',name:'bookedit',component:Bookedit}
		]
	}
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});
//路由守卫
//跳转之前判断当前用户是否合法
router.beforeEach((to,from,next)=>{
	let role = localStorage.getItem('role');
	//没有限制的正常访问
	if(to.meta.role == undefined || to.meta.role == ''){
		next();
		return;
	}
	//判断角色是否与当前登录者的角色匹配
	if(to.meta.role == role){
		next();
		return;
	}
	//以上操作都不符合，默认返回登录页
	router.replace('/main').catch((error)=>{
		console.log('地址不存在');
	})
})
export default router;
